//
//  Get_Handle_GetWaitHandleDetail_Task.m
//  EpointFrame5
//
//  Created by wangzhigang on 14-2-14.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "Get_Handle_GetWaitHandleDetail_Task.h"
#import "FlowInfoModel.h"
#import "MaterialModel.h"
#import "OperationInfoModel.h"
#import "TargetActivityInfoModel.h"
#import "TransactorModel.h"

@implementation Get_Handle_GetWaitHandleDetail_Task

-(void)execute
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *url = self.getMobileOAWebserviceURL;
    NSString *wsNamespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    NSString *userguid = [userDefaults objectForKey:Epoint_User_UserGuid];
    NSString *MessageItemGuid = [params objectForKey:@"MessageItemGuid"];
    
    EpointWebServiceUtil *webserviceHelper = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"Handle_GetWaitHandleDetail"];
    NSString *ParasXml = @"<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><MessageItemGuid>%@</MessageItemGuid></paras>";
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,MessageItemGuid];
    ParasXml = [EpointStringUtil filterXmlString:ParasXml];
    [webserviceHelper addParameterForString:@"ValidateData" value:ValidateData];
    [webserviceHelper addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs = webserviceHelper.start;
    if ([self validateXMLWS:bs]) {
        [self exeSuccessWithData:bs];
    }
}

+(NSMutableArray *)getFlowInfoDataArray:(NSString *)_xml
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:_xml xmlTag:@"FlowInfoLst"];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"FlowInfo"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    
    for (GDataXMLElement *arr in arrays)
    {
        FlowInfoModel *m = [[FlowInfoModel alloc]init];
        
        GDataXMLElement *ActivityName = [[arr elementsForName:@"ActivityName"] objectAtIndex:0];
        m.ActivityName = [ActivityName stringValue];
        
        GDataXMLElement *HandleUserName = [[arr elementsForName:@"HandleUserName"] objectAtIndex:0];
        m.HandleUserName = [HandleUserName stringValue];
        
        GDataXMLElement *ReceiveDateTime = [[arr elementsForName:@"ReceiveDateTime"] objectAtIndex:0];
        m.ReceiveDateTime = [ReceiveDateTime stringValue];
        
        GDataXMLElement *OperationName = [[arr elementsForName:@"OperationName"] objectAtIndex:0];
        m.OperationName = [OperationName stringValue];
        
        GDataXMLElement *HandleDateTime = [[arr elementsForName:@"HandleDateTime"] objectAtIndex:0];
        m.HandleDateTime = [HandleDateTime stringValue];
        
        GDataXMLElement *TransactorName = [[arr elementsForName:@"TransactorName"] objectAtIndex:0];
        m.TransactorName = [TransactorName stringValue];
        
        GDataXMLElement *Opinion = [[arr elementsForName:@"Opinion"] objectAtIndex:0];
        m.Opinion = [Opinion stringValue];
        
        [listmp addObject:m];
    }
    
    return listmp;
}

+(NSMutableArray *)getMeterialDataArray:(NSString *)_xml
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:_xml xmlTag:@"MaterialList"];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"MaterialInfo"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    
    for (GDataXMLElement *arr in arrays)
    {
        MaterialModel *m = [[MaterialModel alloc]init];
        
        GDataXMLElement *MaterialInstanceGuid = [[arr elementsForName:@"MaterialInstanceGuid"] objectAtIndex:0];
        m.MaterialInstanceGuid = [MaterialInstanceGuid stringValue];
        
        GDataXMLElement *MaterialInstanceName = [[arr elementsForName:@"MaterialInstanceName"] objectAtIndex:0];
        m.MaterialInstanceName = [MaterialInstanceName stringValue];
        
        GDataXMLElement *MaterialType = [[arr elementsForName:@"MaterialType"] objectAtIndex:0];
        m.MaterialType = [MaterialType stringValue];
        
        GDataXMLElement *isBigFile = [[arr elementsForName:@"isBigFile"] objectAtIndex:0];
        m.isBigFile = [isBigFile stringValue];
        
        GDataXMLElement *DownLoadUrl = [[arr elementsForName:@"DownLoadUrl"] objectAtIndex:0];
        m.DownLoadUrl = [DownLoadUrl stringValue];
        
        NSLog(@"type:%@",m.MaterialType);
        
        if (![m.MaterialType isEqualToString:@"Form"]) {
            [listmp addObject:m];
        }
    }
    
    return listmp;
}

+(NSMutableArray *)getOperationInfoDataArray:(NSString *)_xml
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:_xml xmlTag:@"OperationList"];
    
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"OperationInfo"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        OperationInfoModel *m = [[OperationInfoModel alloc]init];
        
        GDataXMLElement *OperationGuid = [[arr elementsForName:@"OperationGuid"] objectAtIndex:0];
        m.OperationGuid = [OperationGuid stringValue];
        
        GDataXMLElement *OperationName = [[arr elementsForName:@"OperationName"] objectAtIndex:0];
        m.OperationName = [OperationName stringValue];
        
        GDataXMLElement *Is_RequireOpinion = [[arr elementsForName:@"Is_RequireOpinion"] objectAtIndex:0];
        m.Is_RequireOpinion = [Is_RequireOpinion stringValue];
        
        GDataXMLElement *Is_ShowOperationPage = [[arr elementsForName:@"Is_ShowOperationPage"] objectAtIndex:0];
        m.Is_ShowOperationPage = [Is_ShowOperationPage stringValue];
        
        GDataXMLElement *DefaultOpinion = [[arr elementsForName:@"DefaultOpinion"] objectAtIndex:0];
        m.DefaultOpinion = [DefaultOpinion stringValue];
        
        GDataXMLElement *OperationType = [[arr elementsForName:@"OperationType"] objectAtIndex:0];
        m.OperationType = [OperationType stringValue];
        
        GDataXMLElement *IsSongPY = [[arr elementsForName:@"IsSongPY"] objectAtIndex:0];
        m.IsSongPY = [IsSongPY stringValue];
        
        GDataXMLElement *PYNeedUsers = [[arr elementsForName:@"PYNeedUsers"] objectAtIndex:0];
        m.PYNeedUsers = [PYNeedUsers stringValue];
        
        GDataXMLElement *TargetActivityList = [[arr elementsForName:@"TargetActivityList"] objectAtIndex:0];
        NSArray *TargetActivityInfoArrays = [TargetActivityList elementsForName:@"TargetActivityInfo"];
        NSMutableArray *TargetActivityInfoList = [[NSMutableArray alloc]init];
        for (GDataXMLElement *TargetActivityInfoArray in TargetActivityInfoArrays)
        {
            TargetActivityInfoModel *taim = [[TargetActivityInfoModel alloc]init];
            
            GDataXMLElement *ActivityGuid = [[TargetActivityInfoArray elementsForName:@"ActivityGuid"] objectAtIndex:0];
            taim.ActivityGuid = [ActivityGuid stringValue];
            
            GDataXMLElement *ActivityName = [[TargetActivityInfoArray elementsForName:@"ActivityName"] objectAtIndex:0];
            taim.ActivityName = [ActivityName stringValue];
            
            GDataXMLElement *Is_TargetTransactor_Editable = [[TargetActivityInfoArray elementsForName:@"Is_TargetTransactor_Editable"] objectAtIndex:0];
            taim.Is_TargetTransactor_Editable = [Is_TargetTransactor_Editable stringValue];
            
            GDataXMLElement *TransactorAllowMaxCount = [[TargetActivityInfoArray elementsForName:@"TransactorAllowMaxCount"] objectAtIndex:0];
            taim.TransactorAllowMaxCount = [TransactorAllowMaxCount stringValue];
            
            GDataXMLElement *Is_ShowSelectUser = [[TargetActivityInfoArray elementsForName:@"Is_ShowSelectUser"] objectAtIndex:0];
            taim.Is_ShowSelectUser = [Is_ShowSelectUser stringValue];
            
            NSArray *DefaultTransactorListArrays = [[[TargetActivityInfoArray elementsForName:@"DefaultTransactorList"]objectAtIndex:0]elementsForName:@"Transactor"];
            
            NSMutableArray *DefaultTransactorArraysList = [[NSMutableArray alloc]init];
            for (GDataXMLElement *DefaultTransactorListArray in DefaultTransactorListArrays) {
                TransactorModel *tm = [[TransactorModel alloc]init];
                tm.UserGuid = [[DefaultTransactorListArray attributeForName:@"UserGuid"] stringValue];
                tm.DisplayName = [DefaultTransactorListArray stringValue];
                [DefaultTransactorArraysList addObject:tm];
            }
            taim.DefaultTransactorList = DefaultTransactorArraysList;
            
            NSArray *TransactorFilterListArrays = [[[TargetActivityInfoArray elementsForName:@"TransactorFilterList"]objectAtIndex:0]elementsForName:@"Transactor"];
            
            NSMutableArray *TransactorFilterDataArray = [[NSMutableArray alloc]init];
            
            for (GDataXMLElement *TransactorFilterListArray in TransactorFilterListArrays) {
                TransactorModel *tm = [[TransactorModel alloc]init];
                tm.UserGuid = [[TransactorFilterListArray attributeForName:@"UserGuid"] stringValue];
                tm.DisplayName = [TransactorFilterListArray stringValue];
                [TransactorFilterDataArray addObject:tm];
            }
            
            taim.TransactorFilterList = TransactorFilterDataArray;
            
            [TargetActivityInfoList addObject:taim];
        }
        
        m.TargetActivityList = TargetActivityInfoList;
        [listmp addObject:m];
    }
    return listmp;
}


@end
